﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESF.SSO.Core.Entities;
using System.Data;
using ESF.General;
using ESF.General.Entity;

#endregion


namespace ESF.SSO.Core.DataAccess
{
	public static class AdoTerminal
	{
		#region Read
		
		public static Terminal Get (Terminal pTerminal)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetTerminal");
			
			AdoESFSSO.R.AddParameter (cmd, "pTerminalCode", pTerminal.Code);
			
			return AdoESFSSO.R.Get<Terminal> (cmd, ConvertTo);
		}


		public static List<Terminal> GetTerminals(BasicReference pBR)
		{
			return GetTerminals (pBR, true);
		}


		public static List<Terminal> GetTerminals<BR> (BR pBR, bool pLoadEnterprise) where BR : BasicReference
		{
			IDbCommand cmd;

			
			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetBasicReferenceTerminals");

			AdoESFSSO.R.AddIDParameter (cmd, "pBasicReferenceID", pBR);

			if (pLoadEnterprise)
			{
				return AdoESFSSO.R.GetList <Terminal> (cmd, ConvertToWithEnterprise);
			}

			return AdoESFSSO.R.GetList<Terminal>(cmd, ConvertTo);
		}


		public static Terminal ConvertToWithEnterprise(IDataReader pReader, string pPrefix = "", string pListLoading = "")
		{
			return ConvertTo (pReader, pPrefix, "Enterprise");
		}

		
		public static Terminal ConvertTo (IDataReader pReader, string pPrefix = "", string pListLoading = "")
		{
			Terminal terminal;
			
			
			if (pReader == null) { return null; }
			
			terminal = DataHelper.ConvertToBasicReference <Terminal> (pReader, pPrefix);
			
			if (terminal != null)
			{			
				terminal.Location = AdoLocation.ConvertTo (pReader, pPrefix + "Ter_");
				
				if (!string.IsNullOrWhiteSpace (pListLoading) && pListLoading.Contains ("Enterprise"))
				{
					terminal.Enterprise = AdoEnterprise.ConvertTo (pReader, pPrefix + "Ter_");
				}
			}
			
			return terminal;
		}
		
		#endregion
	}
}
